"""
分类视图 - 门户网站
"""

from rest_framework.views import APIView
from rest_framework.response import Response
from pecco_site.models import Category
from ..serializers import CategorySerializer


class CategoryListView(APIView):
    """
    分类列表 API
    
    GET /api/shop/categories/?locale=en
    """
    permission_classes = []  # 公开访问
    
    def get(self, request):
        locale = request.GET.get('locale', 'en')
        
        categories = []
        category_items = Category.objects.filter(
            is_active=True
        ).order_by('sort_order')
        
        for cat in category_items:
            translation = cat.translations.filter(locale=locale).first()
            if translation:
                category_data = {
                    'id': cat.id,
                    'slug': cat.slug,
                    'name': translation.name,
                    'icon': request.build_absolute_uri(cat.icon.url) if cat.icon else None,
                }
                categories.append(category_data)
        
        return Response({
            'results': categories
        })

